package com.viewpagertext.utils;

import java.util.Locale;

public class TimeUtil {
    public static String formatSeconds(long seconds){
        String standardTime;
        if (seconds <= 0){
            standardTime = "00:00";
        } else if (seconds < 60) {
            standardTime = String.format(Locale.getDefault(), "00:%02d", seconds % 60);
        } else if (seconds < 3600) {
            standardTime = String.format(Locale.getDefault(), "%02d:%02d", seconds / 60, seconds % 60);
        } else {
            standardTime = String.format(Locale.getDefault(), "%02d:%02d:%02d", seconds / 3600, seconds % 3600 / 60, seconds % 60);
        }
        return standardTime;
    }
    public static long timeToSeconds(String time) {
        // 假设时间格式为 "X分Y秒"
        String[] parts = time.split(":"); // 分割分钟和秒数
        if (parts.length != 2) {
            throw new IllegalArgumentException("Invalid time format. Expected X分Y秒");
        }

        int minutes = Integer.parseInt(parts[0]);
        String secondsStr = parts[1]; // 移除“秒”字并获取秒数
        int seconds = Integer.parseInt(secondsStr);

        if (minutes < 0 || seconds < 0 || seconds > 59) {
            throw new IllegalArgumentException("Invalid time value. Minutes must be non-negative, seconds between 0 and 59.");
        }

        // 转换为总秒数
        long totalSeconds = minutes * 60 + seconds;
        return totalSeconds;
    }


    //时间显示函数,我们获得音乐信息的是以毫秒为单位的，把把转换成我们熟悉的00:00格式
    public static String ShowTime(int time) {
        time /= 1000;
        int minute = time / 60;
        int hour = minute / 60;
        int second = time % 60;
        minute %= 60;
        return String.format("%02d:%02d", minute, second);
    }
}
